<?php
declare (strict_types=1);

namespace app\model\user;

use think\Model;
use crmeb\basic\BaseModel;
use crmeb\traits\ModelTrait;
use think\model\relation\BelongsTo;


/**
 * @property int $expire_time 过期时间
 * @property int $id
 * @property int $user_id 用户id
 * @property string $create_time 创建时间
 * @property string $device_type 设备类型;mobile,android,iphone,ipad,web,pc,mac,wxapp
 * @property string $token token
 * @property-read \app\model\user\User $user
 */
class UserToken extends BaseModel
{
    /**
     * 数据表主键
     * @var string
     */
    protected $pk = 'id';

    /**
     * 模型名称
     * @var string
     */
    protected $name = 'user_token';

    use ModelTrait;

    protected $autoWriteTimestamp = true;
    protected $createTime = 'create_time';
    protected $updateTime = false;

    /**
     * 用户id
     * @param Model $query
     * @param $value
     */
    public function searchUserIdAttr($query, $value)
    {
        if ($value) {
            $query->where('user_id', $value);
        }
    }

    /**
     * token
     * @param Model $query
     * @param $value
     */
    public function searchTokenAttr($query, $value)
    {
        if ($value) {
            $query->where('token', $value);
        }
    }

    /**
     * 设备类型;mobile,android,iphone,ipad,web,pc,mac,wxapp
     * @param Model $query
     * @param $value
     */
    public function searchDeviceTypeAttr($query, $value)
    {
        if ($value) {
            $query->where('device_type', $value);
        }
    }


    /**
     * @return BelongsTo
     */
    public function user(): BelongsTo
    {
        return $this->belongsTo(User::class, 'user_id', 'id');
    }
}
